# coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')


# 输出分页方法
def page_code_html(data, page, route_path, pagecount):
    """

    :param data: 搜索条件字典
    :param page:  当前页码数 int类型
    :param route_path: 当前页面路由
    :param pagecount: 总页数 int类型
    :return:
    """
    s = ''
    if pagecount == 0:
        return s
    for d in data:
        if data[d]:
            s += '&' + str(d) + '=' + str(data[d])
    q = '<a class="ui-pagination-page-item" data-current="1" href="' + route_path
    e = '</a>'
    p = '<a href="javascript:void(0);" class="ui-pagination-page-item active" data-current="2">'
    m = ''
    if page > 1:
        if s != '':
            m += q + '?p=1' + s + '">' + u"首页" + e
        else:
            m += q + '?p=1' + '">' + u"首页" + e
    if page - 2 >= 1:
        if s != '':
            m += q + '?p=' + str(page - 2) + s + '">' + str(page - 2) + e
        else:
            m += q + '?p=' + str(page - 2) + '">' + str(page - 2) + e
    if page - 1 >= 1:
        if s != '':
            m += q + '?p=' + str(page - 1) + s + '">' + str(page - 1) + e
        else:
            m += q + '?p=' + str(page - 1) + '">' + str(page - 1) + e
    m += p + str(page) + e
    if page + 1 <= pagecount:
        if s != '':
            m += q + '?p=' + str(page + 1) + s + '">' + str(page + 1) + e
        else:
            m += q + '?p=' + str(page + 1) + '">' + str(page + 1) + e
    if page + 2 <= pagecount:
        if s != '':
            m += q + '?p=' + str(page + 2) + s + '">' + str(page + 2) + e
        else:
            m += q + '?p=' + str(page + 2) + '">' + str(page + 2) + e
    if page < pagecount:
        if s != '':
            m += q + '?p=' + str(pagecount) + s + '">' + u"末页" + e
        else:
            m += q + '?p=' + str(pagecount) + '">' + u"末页" + e
    return m


# POST输出分页方法
def page_code_html_post(page, route_path, pagecount):
    """
    :param page:  当前页码数 int类型
    :param route_path: 调用的AJAX方法
    :param pagecount: 总页数 int类型
    :return:
    """
    s = ''
    if pagecount == 0:
        return s
    q = '<a class="ui-pagination-page-item" data-current="1" onclick="' + route_path
    e = '</a>'
    p = '<a href="javascript:void(0);" class="ui-pagination-page-item active" data-current="2">'
    m = ''
    if page > 1:
        m += q + '(1)' + '">' + u"首页" + e
    if page - 2 >= 1:
        m += q + '(' + str(page - 2) + ')">' + str(page - 2) + e
    if page - 1 >= 1:
        m += q + '(' + str(page - 1) + ')">' + str(page - 1) + e
    m += p + str(page) + e
    if page + 1 <= pagecount:
        m += q + '(' + str(page + 1) + ')">' + str(page + 1) + e
    if page + 2 <= pagecount:
        m += q + '(' + str(page + 2) + ')">' + str(page + 2) + e
    if page < pagecount:
        m += q + '(' + str(pagecount) + s + ')">' + u"末页" + e
    return m


